package com.tcoiss.datafactory.thread;

import com.tcoiss.datafactory.domain.BusTable;
import com.tcoiss.datafactory.service.IDynamicSqlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Future;

@Component
public class SyncThreadHandler {
    @Autowired
    private IDynamicSqlService iDynamicSqlService;

    private static final Logger LOG = LoggerFactory.getLogger(SyncThreadHandler.class);

    @Async
    public Future<String> syncUpdateData(BusTable busTable, String[] tab_fields, List<Object[]> list,boolean isall,int pageIndex) {
        LOG.info(String.format("此批数据的段数为:%s 此段数据的数据条数为:%s", pageIndex, list.size()));
        //声明future对象
        Future<String> result = new AsyncResult<String>("");
        //循环遍历该段旅客集合
        if (null != list && list.size() > 0) {
            try {
                if(isall){
                    iDynamicSqlService.batchInsert(busTable, tab_fields, list);
                }else{
                    iDynamicSqlService.batchUpdateOrInsert(busTable, tab_fields, list);
                }
            } catch (Exception e) {
                //记录出现异常的时间，线程name
                result = new AsyncResult<String>("fail,异常信息：" + e.getMessage() + ",异常线程：" + Thread.currentThread().getName() + ",pageIndex=" + pageIndex);
            }
        }
        return result;

    }

}
